package com.usian.article.lister;

import com.usian.article.controller.ApArticleController;
import com.usian.article.service.IApArticleService;
import com.usian.common.util.JsonUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

import static com.usian.common.constants.message.WmNewsUpDownConstants.WM_NEWS_UP_OR_DOWN_TOPIC;

/**
 * @author Fay
 * @date 2022/7/14&20:00
 */
@Component
public class WmNewsUpOrDownListener {
    @Autowired
    private IApArticleService apArticleService;
    @KafkaListener(topics = WM_NEWS_UP_OR_DOWN_TOPIC)
    public void receiveMsg(ConsumerRecord<?,?> record){
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if (optional.isPresent()){
            Object value = record.value();
            Map<String, Integer> map = JsonUtils.toMap(String.valueOf(value), String.class, Integer.class);
            apArticleService.updateIsDown(map);
        }
    }
}
